import 'package:flutter/material.dart';

class FormPage extends StatefulWidget {
  FormPage({super.key});

  @override
  State<FormPage> createState() => _TextFieldPageState();
}

class _TextFieldPageState extends State<FormPage> {
  //控制器
   late TextEditingController controller;
   //表单key
   final formKey = GlobalKey<FormState>();

   @override
  void initState() {
    super.initState();
    controller = TextEditingController();
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Text'),
      ),
      body: Padding(
        padding:  const EdgeInsets.all(20.0),
        child: Form(
          key: formKey,
          child: Column(children: [
            TextFormField(
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                prefixIcon: Icon(Icons.person),
                hintText: "请输入用户名"
              ),
              validator: (value){
                if(value?.isEmpty == true){
                  return "用户名不能为空";
                }
              },
            ),
            const SizedBox(height: 20,),
            ElevatedButton(onPressed: ()=> login(), child: const Text("登录"))
            
          ],),),
      ),
    );
  }

  login() {
     if(formKey.currentState?.validate() == true){
       formKey.currentState?.save();
     }
  }
}
